package com.bjpowernode.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bjpowernode.shop.base.BaseMember;
import com.bjpowernode.shop.domain.UserAddr;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/p/address")
public class UserAddrController extends BaseMember {

    @GetMapping("/list")
    public ResponseEntity<List<UserAddr>> list() {
        return ResponseEntity.ok(userAddrService.list(
                new LambdaQueryWrapper<UserAddr>().eq(
                        UserAddr::getUserId, getStringUserId()
                ).eq(
                        UserAddr::getStatus, 1
                )
        ));
    }

    @PutMapping("/defaultAddr/{addrId}")
    @Transactional
    public ResponseEntity defaultAddr(@PathVariable Long addrId) {
        // 获取当前选择的地址
        UserAddr selectAddr = userAddrService.getOne(new LambdaQueryWrapper<UserAddr>().eq(
                        UserAddr::getUserId, getStringUserId()
                ).eq(
                        UserAddr::getAddrId, addrId
                )
        );
        if (ObjectUtils.isEmpty(selectAddr))
            throw new RuntimeException("当前收货地址加载异常");

        // 判断是否为误操作[判断当前选择地址是否已经为默认地址]
        if (selectAddr.getCommonAddr().equals(1)) {
            return ResponseEntity.ok("当前选择地址已经是默认地址");
        }

        // ==不是误操作，则开始修改==
        // 将原默认地址的标识改为0
        UserAddr oldDefaultAddr = userAddrService.getOne(new LambdaQueryWrapper<UserAddr>().eq(
                UserAddr::getUserId, getStringUserId()
        ).eq(
                UserAddr::getCommonAddr, 1
        ));
        oldDefaultAddr.setCommonAddr(0).setUpdateTime(new Date());
        userAddrService.updateById(oldDefaultAddr);

        // 将当前选择改为默认
        selectAddr.setCommonAddr(1).setUpdateTime(new Date());
        userAddrService.updateById(selectAddr);

        return ResponseEntity.ok("默认地址更改成功");
    }

    // 新增地址
    @PostMapping("/addAddr")
    @Transactional
    public ResponseEntity<Boolean> save(@RequestBody UserAddr userAddr) {
        // 查询当前用户是否有默认地址
        UserAddr one = userAddrService.getOne(new LambdaQueryWrapper<UserAddr>()
                .eq(UserAddr::getUserId, getStringUserId())
                .eq(UserAddr::getCommonAddr, 1)
        );

        return ResponseEntity.ok(userAddrService.save(
                userAddr.setStatus(1)
                        .setUserId(getStringUserId())
                        .setCommonAddr(
                                ObjectUtils.isEmpty(one) ? 1 : 0
                        ).setCreateTime(new Date())
                        .setUpdateTime(new Date())
                        .setVersion(1)
        ));
    }

    // 展示地址原信息
    @GetMapping("/addrInfo/{addrId}")
    public ResponseEntity<UserAddr> info(@PathVariable Long addrId) {
        return ResponseEntity.ok(userAddrService.getById(addrId));
    }
    
    @PutMapping("/updateAddr")
    public ResponseEntity<Boolean> update(@RequestBody UserAddr userAddr){
        return ResponseEntity.ok(userAddrService.updateById(userAddr.setUpdateTime(new Date())));
    }
    
    @DeleteMapping("/deleteAddr/{addrId}")
    public ResponseEntity<Boolean> del(@PathVariable Long addrId){
        return ResponseEntity.ok(userAddrService.remove(new LambdaQueryWrapper<UserAddr>().eq(
                UserAddr::getAddrId,addrId
        )));
    }
    
    // ------------------远程调用------------------
    @GetMapping("/getDefaultAddr")
    public UserAddr getDefaultAddr(@RequestParam("userId") String userId){
        return userAddrService.getOne(
                new LambdaQueryWrapper<UserAddr>().eq(
                        StringUtils.isNotEmpty(userId),UserAddr::getUserId,userId
                ).eq(
                        UserAddr::getCommonAddr,1
                )
        );                
    }
}
